<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
 * 店铺分类转换至经营范围
 *
 * @version $Id: cat2business.php 4 2011-12-15 09:28:04Z 周永隆 $
 */
class CLI_Cat2business extends CLI
{

    public function main()
    {
        try{
            CLI::output('处理开始');
            
            $select = DB::select('shop_cat_id', 'shop_cat_name')
                ->from('shop_cats')
                ->where('is_show', '=', 1)
                ->fetch_all();
            foreach ($select as $v)
            {
                switch ($v['shop_cat_name'])
                {
                    case '手机':
                        $mp_f_cat_id = $v['shop_cat_id'];
                        break;
                    case '电脑':
                        $pc_f_cat_id = $v['shop_cat_id'];
                        break;
                    case '数码':
                        $e_f_cat_id = $v['shop_cat_id'];
                        break;
                }
            }

            $select = DB::select('business_id', 'business_name')
                ->from('shop_business')
                ->where('deleted', '=', 0)
                ->where('is_show', '=', 1)
                ->fetch_all();
            foreach ($select as $v)
            {
                switch ($v['business_name'])
                {
                    case '手机通讯':
                        $mp_t_cat_id = $v['business_id'];
                        break;
                    case '电脑硬件':
                        $pc_t_cat_id = $v['business_id'];
                        break;
                    case '数码相机':
                        $e_t_cat_id = $v['business_id'];
                        break;
                }
            }

            DB::update('shops')
                ->set(array('shop_business' => DB::expr("CONCAT(shop_business,'".$mp_t_cat_id.",')")))
                ->where('shop_cat_id', '=', $mp_f_cat_id)
                ->execute();
            DB::update('shops')
                ->set(array('shop_business' => DB::expr("CONCAT(shop_business,'".$pc_t_cat_id.",')")))
                ->where('shop_cat_id', '=', $pc_f_cat_id)
                ->execute();
            DB::update('shops')
                ->set(array('shop_business' => DB::expr("CONCAT(shop_business,'".$e_t_cat_id.",')")))
                ->where('shop_cat_id', '=', $e_f_cat_id)
                ->execute();
                
            CLI::output("\n处理完成");
        }
        catch(Exception $e)
        {
            CLI::output("\n".$e->getMessage());
        }
    }

}